package edu.guet.server.service;

import edu.guet.server.model.SemesterStatistic.Student;
import edu.guet.server.model.Stu;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ExcelExportService {
    private List<Stu> processExcel(MultipartFile file) throws IOException {
        List<Stu> students = new ArrayList<>();

        try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
            Sheet sheet = workbook.getSheetAt(0); // 假设只处理第一个Sheet页

            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                if (row.getRowNum() == 0) {
                    // 跳过表头
                    continue;
                }

                // 解析每行数据
                Iterator<Cell> cellIterator = row.cellIterator();
                Stu student = new Stu();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    int columnIndex = cell.getColumnIndex();

                    switch (columnIndex) {
                        case 0:
                            student.setUsername(cell.getStringCellValue());
                            break;
                        case 1:
                            student.setStu_number((String) cell.getStringCellValue());
                            break;
                        // 继续添加其他字段的解析
                        default:
                            break;
                    }
                }
                students.add(student);
            }
        }

        return students;
    }
}
